package net.easyjava.framework.third.rongcloud;

import net.easyjava.framework.utils.ProjectProperties;
import net.easyjava.third.rongcloud.RongCloudClient;
import net.easyjava.third.rongcloud.rong.models.Message;
import net.easyjava.third.rongcloud.rong.models.TxtMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * need project has project.properties
 * Created by wxnacy on 16/7/4.
 */
public class RongCloudUtils {
    static Logger logger = LoggerFactory.getLogger(RongCloudUtils.class);
    private static RongCloudClient client;
    private static ProjectProperties properties;
    private static String appKey;
    private static String appSecret;
    /**
     * 初始化参数
     */
    private static void init(){
        if(client == null){
            initClient();
        }
    }
    private static synchronized void initClient(){
        if(client == null){
            properties = ProjectProperties.getInstance();
            if(appKey == null){
                appKey = properties.getProperty(RongCloudConstants.PROPERTIES_KEY_NAME);
//                throw new NullPointerException("appkey can't be null");
            }
            if(appSecret == null){
                appSecret = properties.getProperty(RongCloudConstants.PROPERTIES_SECRET_NAME);
//                throw new NullPointerException("appSecret can't be null");
            }

            client = RongCloudClient.getInstance(appKey,appSecret);
        }
    }
    /**
     * 获取用户token
     * @param userId
     * @param userName
     * @param faceUrl
     * @return
     */
    public static String getToken(String userId,String userName,String faceUrl){
        try {
            init();
            return client.getToken(userId,userName,faceUrl);
        } catch (Exception e) {
            logger.error("融云获取用户token失败,userId:{},userName:{},faceUrl:{}",userId,userName,faceUrl);
            return null;
        }
    }

    /**
     * 创建聊天室
     * @param chatroomId
     * @param chatroomName
     * @return
     */
    public static boolean createChatroom(String chatroomId,String chatroomName){
        try {
            init();
            return client.createChatroom(chatroomId, chatroomName);
        } catch (Exception e) {
            logger.error("融云创建聊天室失败,chatroomId:{},chatroomName:{}",chatroomId,chatroomName);
            return false;
        }
    }

    /**
     * 拉黑用户
     * @param userId
     * @param blackUser
     * @return
     */
    public static boolean blackUser(String userId,String blackUser){
        return blackUser(userId, Arrays.asList(blackUser));
    }
    public static boolean blackUser(String userId,List<String> blackUsers){
        try {
            init();
            return client.blackUser(userId, blackUsers);
        } catch (Exception e) {
            logger.error("融云拉黑用户失败,userId:{},blackUsers:{}",userId,blackUsers);
            return false;
        }
    }

    /**
     * 移除拉黑用户
     * @param userId
     * @param blackUser
     * @return
     */
    public static boolean removeBlackUser(String userId,String blackUser){
        return removeBlackUser(userId, Arrays.asList(blackUser));
    }
    public static boolean removeBlackUser(String userId,List<String> blackUsers){
        try {
            init();
            return client.removeBlackUser(userId, blackUsers);
        } catch (Exception e) {
            logger.error("融云移除拉黑用户失败,userId:{},blackUsers:{}",userId,blackUsers);
            return false;
        }
    }

    /**
     * 获取拉黑用户
     * @param userId
     * @return
     */
    public static List<String> getBlackUids(String userId){
        try {
            init();
            return client.getBlackUids(userId);
        } catch (Exception e) {
            logger.error("融云获取拉黑用户失败,userId:{}",userId);
            return new ArrayList<>();
        }
    }

    /**
     * 封禁用户
     * @param userId
     * @param minute 封禁时长
     * @return
     */
    public static boolean blockUser(String userId,int minute){
        try {
            init();
            return client.blockUser(userId, minute);
        } catch (Exception e) {
            logger.error("融云封禁用户失败,userId:{},minute:{}",userId,minute);
            return false;
        }
    }

    /**
     * 解除封禁
     * @param userId
     * @return
     */
    public static boolean unBlockUser(String userId){
        try {
            init();
            return client.unBlockUser(userId);
        } catch (Exception e) {
            logger.error("融云解除封禁用户失败,userId:{}",userId);
            return false;
        }
    }

    /**
     * 发送聊天室消息
     * @param fromUserId
     * @param chatroomId
     * @param content
     * @return
     */
    public static boolean sendChatroomMessage( String fromUserId,String chatroomId,String content){
        return sendChatroomMessage(fromUserId, Arrays.asList(chatroomId), content);
    }
    public static boolean sendChatroomMessage( String fromUserId,List<String> chatroomIds,String content){
        try {
            init();
            return client.sendChatroomMessage(fromUserId, chatroomIds, content);
        } catch (Exception e) {
            logger.error("融云发送聊天室消息失败,fromUserId:{},chatroomIds:{},content:{}",fromUserId,chatroomIds,content);
            return false;
        }
    }


    /**
     * 发送系统消息
     * @param fromUserId
     * @param toUserId
     * @param content
     * @param pushContent
     * @return
     */
    public static boolean sendSystemMessage( String fromUserId,String toUserId,String content,String pushContent){
        return sendSystemMessage(fromUserId, Arrays.asList(toUserId), content, pushContent);
    }
    public static boolean sendSystemMessage( String fromUserId,List<String> toUserIds,String content,String pushContent){
        return sendSystemMessage(fromUserId, toUserIds, new TxtMessage(content), pushContent);
    }
    public static boolean sendSystemMessage( String fromUserId,String toUserId,Message message,String pushContent){
        return sendSystemMessage(fromUserId, Arrays.asList(toUserId), message, pushContent);
    }
    public static boolean sendSystemMessage( String fromUserId,List<String> toUserIds,Message message,String pushContent){
        try {
            init();
            return client.sendSystemMessage(fromUserId,toUserIds,message,pushContent);
        } catch (Exception e) {
            logger.error("融云发送聊天室消息失败,fromUserId:{},toUserIds:{},message:{},pushContent:{}",fromUserId,toUserIds,message,pushContent);
            return false;
        }
    }


    /**
     * 向全体用户发送广播推送
     * @param fromUserId
     * @param alert
     * @param message
     * @return
     */
    public static boolean sendBroadcastPushToAll( String fromUserId,String alert,Message message){
        try {
            init();
            return client.sendBroadcastPushToAll(fromUserId, alert, message);
        } catch (Exception e) {
            logger.error("融云发送广播推送消息失败,fromUserId:{},alert:{},message:{}",fromUserId,alert,message);
            return false;
        }
    }
    public static boolean sendBroadcastPushToIOS( String fromUserId,String alert,Message message){
        try {
            init();
            return client.sendBroadcastPushToIOS(fromUserId, alert, message);
        } catch (Exception e) {
            logger.error("融云发送IOS广播推送消息失败,fromUserId:{},alert:{},message:{}",fromUserId,alert,message);
            return false;
        }
    }
    public static boolean sendBroadcastPushToAndroid( String fromUserId,String alert,Message message){
        try {
            init();
            return client.sendBroadcastPushToAndroid(fromUserId, alert, message);
        } catch (Exception e) {
            logger.error("融云发送android广播推送消息失败,fromUserId:{},alert:{},message:{}",fromUserId,alert,message);
            return false;
        }
    }
    public static boolean sendBroadcastPushToTags( String fromUserId,String alert,Message message,String... tags){
        try {
            init();
            return client.sendBroadcastPushToTags(fromUserId, alert, message, tags);
        } catch (Exception e) {
            logger.error("融云发送标签广播推送消息失败,fromUserId:{},alert:{},message:{},tags:{}",fromUserId,alert,message,Arrays.toString(tags));
            return false;
        }
    }

    /**
     * 设置标签
     * @param userId
     * @param tags
     * @return
     */
    public static boolean setTags( String userId,String... tags){
        try {
            init();
            return client.setTags(userId, tags);
        } catch (Exception e) {
            logger.error("融云设置标签失败,userId:{},tags:{}",userId,Arrays.toString(tags));
            return false;
        }
    }













}
